#include "PrecompiledHeader.h"
#include "IRenderModelMatrix.h"

/*--------------------------------------------------------------------------*/

IRenderModelMatrix::IRenderModelMatrix() :
	IRender(ERenderFlag::None)
{
	this->m_modelMatrixNeedsRecalc = true;
	this->m_ModelQuaternionRotation = glm::quat();
	this->m_ModelScale = glm::vec3(1.0f, 1.0f, 1.0f);
	this->m_ModelTrans = glm::vec3(0.0f, 0.0f, 0.0f);
	this->m_ModelMatrix = glm::mat4();
}

/*--------------------------------------------------------------------------*/

IRenderModelMatrix::~IRenderModelMatrix()
{

}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::SetScale(float scale)
{
	if (scale != this->m_ModelScale.x || scale != this->m_ModelScale.y || scale != this->m_ModelScale.z)
	{
		this->m_modelMatrixNeedsRecalc = true;
		this->m_ModelScale.x = this->m_ModelScale.y = this->m_ModelScale.z = scale;
	}
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::SetScale(const glm::vec3& scale)
{
	if (scale != this->m_ModelScale)
	{
		this->m_modelMatrixNeedsRecalc = true;
		this->m_ModelScale = scale;
	}
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::SetTranslation(const glm::vec3& translate)
{
	if (translate != this->m_ModelTrans)
	{
		this->m_modelMatrixNeedsRecalc = true;
		this->m_ModelTrans = translate;
	}
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::SetRotation(const glm::vec3& rotation)
{
	this->m_modelMatrixNeedsRecalc = true;

	static glm::quat q;
	this->m_ModelQuaternionRotation = glm::quat();

	q = glm::angleAxis(rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
	this->m_ModelQuaternionRotation = this->m_ModelQuaternionRotation * q;

	q = glm::angleAxis(rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
	this->m_ModelQuaternionRotation = this->m_ModelQuaternionRotation * q;

	q = glm::angleAxis(rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
	this->m_ModelQuaternionRotation = this->m_ModelQuaternionRotation * q;
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::Zero()
{
	this->m_modelMatrixNeedsRecalc = true;
	this->m_ModelQuaternionRotation = glm::quat();
	this->m_ModelScale = glm::vec3(1.0f, 1.0f, 1.0f);
	this->m_ModelTrans = glm::vec3(0.0f, 0.0f, 0.0f);
	this->m_ModelMatrix = glm::mat4();
}

/*--------------------------------------------------------------------------*/

const glm::vec3& IRenderModelMatrix::GetScale() const
{
	return this->m_ModelScale;
}

/*--------------------------------------------------------------------------*/

const glm::vec3& IRenderModelMatrix::GetTranslation() const
{
	return this->m_ModelTrans;
}

/*--------------------------------------------------------------------------*/

const glm::quat& IRenderModelMatrix::GetRotation() const
{
	return this->m_ModelQuaternionRotation;
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::CalcModelMatrices()
{
	if (this->m_modelMatrixNeedsRecalc)
	{
		// od prawa do lewa
		this->m_ModelMatrix = glm::translate(this->m_ModelTrans) * glm::scale(this->m_ModelScale) * glm::toMat4(this->m_ModelQuaternionRotation);
		this->m_modelMatrixNeedsRecalc = false;
	}
}

/*--------------------------------------------------------------------------*/

const glm::mat4& IRenderModelMatrix::GetModelMatrix() const
{
	return this->m_ModelMatrix;
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::RotateX(float val)
{
	this->m_ModelQuaternionRotation = this->m_ModelQuaternionRotation * glm::angleAxis(val, glm::vec3(1.0f, 0.0f, 0.0f));
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::RotateY(float val)
{
	this->m_ModelQuaternionRotation = this->m_ModelQuaternionRotation * glm::angleAxis(val, glm::vec3(0.0f, 1.0f, 0.0f));
}

/*--------------------------------------------------------------------------*/

void IRenderModelMatrix::RotateZ(float val)
{
	this->m_ModelQuaternionRotation = this->m_ModelQuaternionRotation * glm::angleAxis(val, glm::vec3(0.0f, 0.0f, 1.0f));
}

/*--------------------------------------------------------------------------*/
